/**
 * Created by admin on 2017/4/8.
 */

window.onload=function () {
    var http = "http://new.yytaomeng.com";
    /*------------------------贷款头部数据-------------------------*/
    var headerData = {
        "loans_scope": [{
            "loans_scope_num": '1',
            "loans_scope": '金额不限'
        }, {
            "loans_scope_num": '2',
            'loans_scope': '0-2000'
        }, {
            "loans_scope_num": '3',
            'loans_scope': '2000-5000'
        }, {
            "loans_scope_num": '4',
            'loans_scope': '5000-10000'
        }, {
            "loans_scope_num": '5',
            'loans_scope': '10000以上'
        }],
        "loans_type": [{
            'loans_type_num': '2',
            "loans_type": "产品类型"
        }, {
            'loans_type_num': '1',
            "loans_type": "信用卡贷"
        }],
        "term_range": [{
            'term_range_num': '1',
            "term_range": "期限不限"
        }, {
            'term_range_num': '2',
            "term_range": "1-6"
        }, {
            'term_range_num': '3',
            "term_range": "7-12"
        }, {
            'term_range_num': '4',
            "term_range": "13-18"
        }, {
            'term_range_num': '5',
            "term_range": "19-24"
        }, {
            'term_range_num': '6',
            "term_range": "25-30"
        }, {
            'term_range_num': '7',
            "term_range": "31-36"
        }]
    };
    var html = "";

    /*function for_data(data){
        for(var i in data){
            var de=data[i];
            for(var j in de){
                if(de[j]==""){
                    console.log(i+"："+j);
                }
            }
        }
    }
    for_data(data);*/
    /*--------------------贷款头部数据加载-------------------------*/
    function header_menu() {
        var header = headerData;
        var loans_scope = "";
        var loans_type = "";
        var term_range = "";
        for (var i in header.loans_scope) {
            var loans_scopes = header.loans_scope[i];
            loans_scope += '<span class="head_block" about="' + loans_scopes.loans_scope_num + '">' + loans_scopes.loans_scope + '</span>';
        }
        for (var k in header.loans_type) {
            var loans_types = header.loans_type[k];
            loans_type += '<span class="head_block" about="' + loans_types.loans_type_num + '">' + loans_types.loans_type + '</span>';
        }
        for (var j in header.term_range) {
            var term_ranges = header.term_range[j];
            term_range += '<span class="head_block" about="' + term_ranges.term_range_num + '">' + term_ranges.term_range + '</span>';
        }
        $('.which_money').html(loans_scope);
        $('.which_kind').html(loans_type);
        $('.which_order').html(term_range);
    }

    header_menu();
    function getimg(img){
        if(img.startsWith("http")){
            return img;
        }else{
            return http+img;
        }
    }
    /*-------------------数据绑定----------------------------------*/

    function countList(daikuan) {
        html = "";
        var strstart1 = '';
        var strstart2 = '';
        for (var a = 0; a < daikuan.start; a++) {
            strstart1 += '<img src="img/img-star@3x.png"/>'
        }
        for (var b = 0; b < (5 - daikuan.start); b++) {
            strstart2 += '<img src="img/img-star-half@3x.png"/>'
        }
        // href="' + daikuan.url + '"
        html += '<li class="dkx_etail_item" >' +
            '<a class="disFlex" href="' + daikuan.url + '" >' +
            '<img src="' + getimg(daikuan.logo)+ '"/>' +
            '<div class="dkx_etail_item_right">' +
            '<div>' +
            '<div id="productName">' + daikuan.name + '</div>' +
            '<div id="productStart">' + strstart1 + strstart2 + '</div>' +
            '</div>' +
            '<div class="product_item_count">' +
            '<div class="download_num">' +
            '<span>借款范围</span>' +
            '<span class="rate_of_interest_detail">&yen;' + daikuan.loans_scope + '</span>' +
            '</div>' +
            '<div class=" rate_of_interest">' +
            '<span >月利率</span>' +
            '<span class="rate_of_interest_detail">' + daikuan.rate + '</span>' +
            '</div>' +
            '<div class="dkx_date">' +
            '<span>放款时间</span>' +
            '<span>' + daikuan.loan_time + '</span>' +
            '</div>' +
            '</div>' +
            '</div>' +
            '</a>' +
            '</li>';

       $('#dkxDetailItems').append(html);
        var ht= $('#dkxDetailItems').html();
        if(ht==""){
            $('.null_item').css("display","block");
        }else{
            $('.null_item').css("display","none");
        }
    }

    /*--------------------当贷款类型为空------------------------*/
    function infoReuse() {
        var data=product.productInfo;
        //条件查询中贷款期限条件
        var d_ordera = parseInt($("#which_order").attr("about"));
        //条件查询中的金额
        var d_money = parseInt($("#which_money").attr("about"));
        if (d_ordera == 1) {    //贷款期限无
            if (d_money == 1) { //金额无
                for (var q in data) {
                    var d_order = data[q];
                    countList(d_order);
                }
            } else {
                //h获取条件中金额的区域值
                var which_money = $('#which_money').html().split("-");

                for (var q in data) {
                    var d_order = data[q];

                    //获取数据中金额的值
                    var money = d_order.loans_scope.split("-");
                    var money_num = parseInt(money[0]);
                    var money_num1 = parseInt(money[1]);

                    if (which_money.length == 1) {
                        if (money_num1 > 10000) {
                            countList(d_order);
                        } else {
                            continue;
                        }
                    } else {
                        var moneyForif = parseInt(which_money[0]);
                        var moneyForif1 = parseInt(which_money[1]);
                        if ((money_num <= moneyForif && moneyForif <= money_num1) || (moneyForif <= money_num && money_num1 <= moneyForif1) || (money_num <= moneyForif1 && moneyForif1 <= money_num1)) {
                            countList(d_order);
                        } else {
                            continue;
                        }
                    }
                }
            }
        } else {
            //获取条件中贷款期限
            var d_ordera_data_if = $("#which_order").html().split("-");
            var d_data = parseInt(d_ordera_data_if[0]);
            var d_data1 = parseInt(d_ordera_data_if[1]);
            for (var a in data) {
                var d_order1 = data[a];
                //获数据中贷款期限

                var d_ordera_data = d_order1.term_range.split("-");
                var d_item_ordera = parseInt(d_ordera_data[0]);
                var d_item_ordera1 = parseInt(d_ordera_data[1]);
                    if ((d_item_ordera <=d_data  && d_data <= d_item_ordera1) || (d_data <= d_item_ordera && d_item_ordera1 <= d_data1) || (d_item_ordera <=d_data1  && d_data1 <=d_item_ordera1 )) {
                    if (d_money == 1) { //金额无
                        countList(d_order1);
                    } else {
                        //h获取条件中金额的区域值
                        var which_money = $('#which_money').html().split("-");
                        //获取数据中金额的值
                        var money = d_order1.loans_scope.split("-");
                        var money_num = parseInt(money[0]);
                        var money_num1 = parseInt(money[1]);

                        if (which_money.length == 1) {
                            if (money_num1 > 10000) {
                                countList(d_order1);
                            }
                        } else {
                            var moneyForif = parseInt(which_money[0]);
                            var moneyForif1 = parseInt(which_money[1]);
                            if ((money_num <= moneyForif && moneyForif <= money_num1) || (moneyForif <= money_num && money_num1 <= moneyForif1) || (money_num <= moneyForif1 && moneyForif1 <= money_num1)) {
                                countList(d_order1);
                                console.log(d_order1.term_range);
                            } else {
                                continue;
                            }
                        }
                    }
                } else {
                    continue;
                }
            }
        }
    }

    /*--------------------当贷款类型不为为空------------------------*/
    function infoReuseNotNull() {
        var which_kind = parseInt($("#which_kind").attr("about"));
        var data=product.productInfo;
        for (var c in data) {
            var item_data = data[c];
            if (item_data.loans_type == which_kind) {
                //条件查询中贷款期限条件
                var d_ordera = parseInt($("#which_order").attr("about"));
                //条件查询中的金额
                var d_money = parseInt($("#which_money").attr("about"));

                //条件查询中贷款无期限条件
                if (d_ordera == 1) {
                    if (d_money == 1) {
                        countList(item_data);
                    } else {
                        var which_money = $('#which_money').html().split("-");
                        //获取数据源中的金额
                        var money = item_data.loans_scope.split("-");
                        var money_num = parseInt(money[0]);
                        var money_num1 = parseInt(money[1]);

                        if (which_money.length == 1) {
                            if (money_num1 > 10000) {
                                countList(item_data);
                            }
                        } else {
                            var moneyForif = parseInt(which_money[0]);
                            var moneyForif1 = parseInt(which_money[1]);
                            if ((money_num <= moneyForif && moneyForif <= money_num1) || (moneyForif <= money_num && money_num1 <= moneyForif1) || (money_num <= moneyForif1 && moneyForif1 <= money_num1)) {
                                countList(item_data);
                            } else {
                                continue;
                            }
                        }
                    }

                    //条件查询中贷款有期限条件
                } else {
                    //获取条件中贷款期限
                    var d_ordera_data_if = $("#which_order").html().split("-");
                    var d_data = parseInt(d_ordera_data_if[0]);
                    var d_data1 = parseInt(d_ordera_data_if[1]);
                    //获数据中贷款期限
                    var d_ordera_data = item_data.term_range.split("-");
                    var d_item_ordera = parseInt(d_ordera_data[0]);
                    var d_item_ordera1 = parseInt(d_ordera_data[1]);
                    if ((d_item_ordera <=d_data  && d_data <= d_item_ordera1) || (d_data <= d_item_ordera && d_item_ordera1 <= d_data1) || (d_item_ordera <=d_data1  && d_data1 <=d_item_ordera1 )) {
                        if (d_money == 1) { //金额无
                            countList(item_data);
                        } else {
                            //h获取条件中金额的区域值
                            var which_money = $('#which_money').html().split("-");
                            //获取数据中金额的值
                            var money = item_data.loans_scope.split("-");
                            var money_num = parseInt(money[0]);
                            var money_num1 = parseInt(money[1]);

                            if (which_money.length == 1) {
                                if (money_num1 > 10000) {
                                    countList(item_data);
                                }
                            } else {
                                var moneyForif = parseInt(which_money[0]);
                                var moneyForif1 = parseInt(which_money[1]);
                                if ((money_num <= moneyForif && moneyForif <= money_num1) || (moneyForif <= money_num && money_num1 <= moneyForif1) || (money_num <= moneyForif1 && moneyForif1 <= money_num1)) {
                                    countList(item_data);
                                } else {
                                    continue;
                                }
                            }
                        }
                    } else {
                        continue;
                    }

                }
            } else {
                continue;
            }
        }

    }

    /*----------------------条件查询按钮操作------------------------*/
    function onloadListInfo() {
        html = "";
        //条件查询中贷款类型条件
        var which_kind_about = parseInt($('#which_kind').attr("about"));
        $('#dkxDetailItems').html("");
        if (which_kind_about == 2) {
            infoReuse();
        } else {
            infoReuseNotNull();
        }

    }

    /*-------------------点击头部菜单显示相应的内容----------------*/
    $('.head_block-items').on("click", function () {
        var id = $(this).children(".head_title").attr("id");
        var cl = "." + id;
        $(cl).css("display", "block");
        $("#titeBg").css("display","block");
    });
    /*------------------------点击详细内容操作---------------------*/
    $('.head_block').on('click', function () {
        var names = $(this).parents('.dkx_nav').attr("id");
        var val = $(this).html();
        var about = $(this).attr("about");
        var ids = "#which_" + names;
        $(ids).html(val);
        $(ids).attr("about", about);
        $(this).parents(".dkx_nav").css("display", "none");
        $(this).addClass("dkx_nav_active").siblings().removeClass("dkx_nav_active");
        $("#titeBg").css("display","none");
        onloadListInfo();
    });
    $("#titeBg").on("click",function(){
        $('.dkx_nav').css("display", "none");
        $(this).css("display", "none");
    });

    function GetQueryString(key) {
        // key为url变量名称
        var urlGets = location.search.substr(1);
        if (urlGets.length > 0) {
            start = urlGets.indexOf(key + '=');
            if (start != -1) {
                start = start + key.length + 1;
                end = urlGets.indexOf('&', start);
                if (end == -1) {
                    end = urlGets.length
                }
                return decodeURIComponent(urlGets.substring(start, end));
            }
        }
        return '';
    }

       function aaa(){
        $('body').append("<div class='lodings'>" +
            "<img src='img/loding.png'>" +
            "</div>");
    }
    aaa();
      /* function displaynone(){
           $(".lodings").css("display","none");
       }*/


       /*$("#dkxDetailItems").on("click",".dkx_etail_item",function(){
            $(this).css("background","#dcdcdc");
           $(this).siblings(".dkx_etail_item").css("background","#ffffff");
        });*/

    //touchstart、touchmove和touchend) - fuqinyijiu的...

        $("#dkxDetailItems").on("touchstart",".dkx_etail_item",function(){
            //$(this).css("background","#dcdcdc");
            $(this).css("box-shadow","0 0 10px #000");
        });
        $("#dkxDetailItems").on("touchend",".dkx_etail_item",function(){
           // $(this).css("background","#ffffff");
            $(this).css("box-shadow","0 0 0 #ffffff");
        });

    /*--------------------------参数名称---------------------------*/
    /*
     *loans_scope贷款金额范围
     *loans_type贷款类型
     *term_range贷款期限
     * */

    /*--------------------对json数据分析加载---------------------*/
    function queryInformationForData(loans_scope, loans_type, term_range) {
        var loans_scope_detail = parseInt(loans_scope);
        var loans_types = parseInt(loans_type);
        var term_ranges = parseInt(term_range);
        var daikuan_detail = product.productInfo;
        html = "";
        $("#dkxDetailItems").html("");
        for (var p in daikuan_detail) {
            var dk_item = daikuan_detail[p];
            if (!loans_types || loans_types == dk_item.loans_type) {
                var dk_tang = dk_item.term_range.split("-");
                dk_tang_num = parseInt(dk_tang[0]);
                dk_tang_num1 = parseInt(dk_tang[1]);
                if (!term_ranges || (dk_tang_num <= term_ranges && term_ranges <= dk_tang_num1)) {
                    var dk_scope = dk_item.loans_scope.split("-");
                    dk_scope_num = parseInt(dk_scope[0]);
                    dk_scope_num1 = parseInt(dk_scope[1]);
                    if (!loans_scope_detail || (dk_scope_num <= loans_scope_detail && loans_scope_detail <= dk_scope_num1)) {
                        countList(dk_item);
                    }
                }
            }
        }
    }

    /*--------------------加载时获取数据-------------------------*/
    function loadingQuery() {
        var toastmsg=GetQueryString("toastmsg");
        if(toastmsg=="1"){
            $('.showMsg').html("很遗憾，你的审核未通过，我们精心为您推荐以下产品！").css("display","block");
        }else{
            $('.showMsg').css("display","none");
        }
        var loans_scope = GetQueryString(loans_scope);
        var loans_type = GetQueryString(loans_type);
        var term_range = GetQueryString(term_range);
        loans_scope = loans_scope || "";
        loans_type = loans_type || "";
        term_range = term_range || "";
        queryInformationForData(loans_scope, loans_type, term_range);

    }
    /*------------------条件选择方法------------------------------*/
    function conditionClick() {
        loadingQuery();
    }
    document.body.addEventListener("touchmove",function(){
        $('.showMsg').css("display","none");
    });
    conditionClick();
};
